import { GraphScript_error_code_path } from "./GraphScript_error_code_path";

var GraphScript_language = {
    "Element": {
        "Language_CFG": {
            "functions": {
                "print": {
                    "params": ["text"],
                    "code": "console.log(text)"
                },
                "input": {
                    "params": ["text"],
                    "code": "prompt(text)"
                },
                "botton":{
                    "params": ["text"],
                    "code": "document.getElementById(text).click()"
                },
                "botton_event":{
                    "params": ["text"],
                    "code": "document.getElementById(text).addEventListener('click',function(){});"
                },
                "draw": {
                    "Draw_Line": {
                        "params": ["x1", "y1", "x2", "y2", "color"],
                        "code": "ctx.beginPath();ctx.moveTo(x1,y1);ctx.lineTo(x2,y2);ctx.strokeStyle=color;ctx.stroke();"
                    },
                    "Draw_circle": {
                        "params": ["x", "y", "radius", "color"],
                        "code": "ctx.beginPath();ctx.arc(x,y,radius,0,2*Math.PI);ctx.fillStyle=color;ctx.fill();"
                    },
                    "Draw_rect": {
                        "params": ["x", "y", "width", "height", "color"],
                        "code": "ctx.beginPath();ctx.rect(x,y,width,height);ctx.fillStyle=color;ctx.fill();"
                    }
                },
                "int":{
                    "params": ["a","assignment","b"],
                    "code": "a=assignment(b)"
                },
                "time":{
                    "params": ["time"],
                    "code": "new Date(time)"
                },
                "date":{
                    "params": ["date"],
                    "code": "new Date(date)"
                },
                "datetime":{
                    "params": ["datetime"],
                    "code": "new Date(datetime)"

                },
                //这个用于加载外部资源库
                "input":{
                    "params": ["text"],
                    "code": "prompt(text)"
                },
                "load":{
                    "params": ["url"],
                    "code": "document.write('<script src=\"' + url + '\"></script>');"
                }
            }
        }
    },
    //32色模式
    colors:{
        "red": "#FF0000",
        "green": "#00FF00",
        "blue": "#0000FF",
        "yellow": "#FFFF00",
        "purple": "#800080",
        "orange": "#FFA500",
        "black": "#000000",
        "white": "#FFFFFF",
        "gray": "#808080",
        "brown": "#A52A2A",
        "pink": "#FFC0CB",
        "cyan": "#00FFFF",
        "lime": "#00FF00",
        "navy": "#000080",
        "maroon": "#800000",
        "olive": "#808000",
        "teal": "#008080",
        "aqua": "#00FFFF",
        "fuchsia": "#FF00FF",
        "gold": "#FFD700",
        "silver": "#C0C0C0",
        "indigo": "#4B0082",
        "violet": "#8A2BE2",
        "coral": "#FF7F50",
        "chartreuse": "#7FFF00",
        "dodgerblue": "#1E90FF",
        "hotpink": "#FF69B4",
        "khaki": "#F0E68C",
        "lavender": "#E6E6FA",
        "mediumspringgreen": "#00FA9A",
        "mediumvioletred": "#C71585",
        "olivedrab": "#6B8E23",
        "rosybrown": "#BC8F8F",
        "sandybrown": "#F4A460",
        "seagreen": "#2E8B57",
        "thistle": "#D8BFD8",
        "turquoise": "#40E0D0",
        "violetred": "#D02090",
        "wheat": "#F5DEB3",
        "yellowgreen": "#9ACD32",
    },
};
var debug={
    "log":function(msg){
        console.log(msg);
    }
}

var GraphScript_language = function(language, function_name, params) {
    var codeObj = GraphScript_language[language]?.Language_CFG?.functions?.[function_name];
    if (!codeObj) {
        throw new Error("Function not found");
    }
    if (typeof codeObj.code === "string") {
        return codeObj.code;
    }
    // 处理嵌套函数（如draw下的子函数）
    if (typeof codeObj === "object" && params[0] && codeObj[params[0]]) {
        return codeObj[params[0]].code;
    }
    throw new Error("Invalid function definition");
}
var GraphScript_language_colors = function(language, color_name) {
    var colorObj = GraphScript_language[language]?.colors?.[color_name];
    if (!colorObj) {
        throw new Error("Color not found");
    }
    return colorObj;
}
var VarDate = function(date) {
    // 日期格式校验
    if (!/^\d{4}-\d{2}-\d{2}$/.test(date)) {
        throw new Error("Invalid date format");
    }
    // 日期范围校验
    var year = parseInt(date.substring(0, 4), 10);
    var month = parseInt(date.substring(5, 7), 10);
    var day = parseInt(date.substring(8, 10), 10);
    if (year < 1900 || year > 2100 || month < 1 || month > 12 || day < 1 || day > 31) {
        throw new Error("Invalid date range");
    }
    // 闰年校验
    if (month === 2 && day === 29 && (year % 4 !== 0 || (year % 100 === 0 && year % 400 !== 0))) {
        throw new Error("Invalid date range");
    }
    // 大小月份天数校验
    if (month === 4 || month === 6 || month === 9 || month === 11) {
        if (day > 30) {
            throw new Error("Invalid date range");
        }
    } else {
        if (day > 31) {
            throw new Error("Invalid date range");
        }
    }

    // 2月天数校验
    if (month === 2) {
        if (day > 29) {
            throw new Error("Invalid date range");
        }
        if (day === 28){
            print ("2月28号")
        }
    }
    return date;
}
var VarTime = function(time) {
    // 时间格式校验
    if (!/^\d{2}:\d{2}:\d{2}$/.test(time)) {
        throw new Error("Invalid time format");
    }
    // 时间范围校验
    var hour = parseInt(time.substring(0, 2), 10);
    var minute = parseInt(time.substring(3, 5), 10);
    var second = parseInt(time.substring(6, 8), 10);
    if (hour < 0 || hour > 23 || minute < 0 || minute > 59 || second < 0 || second > 59) {
        throw new Error("Invalid time range");
    }
    return time;
}
var VarDateTime = function(date, time) {
    return date + " " + time;
}
//按钮属性（botton函数）
var VarButton = function(button_name, button_type, button_text, button_x, button_y, button_width, button_height, button_color, button_text_color) {
    return {
        "name": button_name,
        "type": button_type,
        "text": button_text,
        "x": button_x,
        "y": button_y,
        "width": button_width,
        "height": button_height,
        "color": button_color,
        "text_color": button_text_color,
    }
}
//按钮事件（botton_event函数）
var VarButtonEvent = function(button_name, event_type, event_code) {
    return {
        "name": button_name,
        "type": event_type,
        "code": event_code,
    }
}
//定义Draw函数
var VarDraw = function(draw_name, draw_type, draw_code) {
    return {
        "name": draw_name,
        "type": draw_type,
        "code": draw_code,
    }
}
//定义Draw_Line函数
var VarDraw_Line = function(draw_name, draw_type, draw_code) {
    return {
        "name": draw_name,
        "type": draw_type,
        "code": draw_code,
    }
}
//定义Draw_circle函数
var VarDraw_circle = function(draw_name, draw_type, draw_code) {
    return {
        "name": draw_name,
        "type": draw_type,
        "code": draw_code,
    }
}
//定义Draw_rectangle函数
var VarDraw_rectangle = function(draw_name, draw_type, draw_code) {
    return {
        "name": draw_name,
        "type": draw_type,
        "code": draw_code,
    }
}
//定义print函数
var VarPrint = function(print_name, print_type, print_code) {
    return {
        "name": print_name,
        "type": print_type,
        "code": print_code,
    }
}
//定义input函数
var VarInput = function(input_name, input_type, input_code) {
    return {
        "name": input_name,
        "type": input_type,
        "code": input_code,
    }
}
//定义int函数
var VarInt = function(int_name, int_type, int_code) {
    return {
        "name": int_name,
        "type": int_type,
        "code": int_code,
    }
}
//定义time函数
var VarTime = function(time_name, time_type, time_code) {
    return {
        "name": time_name,
        "type": time_type,
        "code": time_code,
    }
}
//定义date函数
var VarDate = function(date_name, date_type, date_code) {
    return {
        "name": date_name,
        "type": date_type,
        "code": date_code,
    }
}
//定义datetime函数
var VarDateTime = function(datetime_name, datetime_type, datetime_code) {
    return {
        "name": datetime_name,
        "type": datetime_type,
        "code": datetime_code,
    }
}
//定义错误
var GraphScript_error = function(error_code, error_msg) {
    return {
        "code": error_code,
        "msg": error_msg,
    }
}
//定义GraphScript_language_error函数
var GraphScript_language_error = function(error_code, error_msg) {
    return {
        "code": error_code,
        "msg": error_msg,
        "path": GraphScript_error_code_path[error_code],
    }
}
//定义load函数
var VarLoad = function(load_name, load_type, load_code) {
    return {
        "name": load_name,
        "type": load_type,
        "code": load_code,
    }
}
//这里可以定义更多函数
var fct={

}